package controller;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import controller.action.Action;
import controller.action.ActionForward;
import controller.action.user.ListUser;

@WebServlet("*.hrd")
public class CamController extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
    public CamController() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doprocess(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doprocess(request, response);
	}
	
	private void doprocess(HttpServletRequest request , HttpServletResponse response) throws ServletException , IOException{
			String RequestURI = request.getRequestURI();
			String contextPath = request.getContextPath();
			String command = RequestURI.substring(contextPath.length());
			ActionForward forward = null;
			Action action = null;
			System.out.println(command);
			switch(command){
					case "/about.hrd":
						forward = new ActionForward("about.jsp",false);
						break;
						

					case "/users.hrd":
						action = new ListUser();
						try {
							forward = action.execute(request, response);
						} catch (Exception e) {
							e.printStackTrace();
						}
						break;
		
			}
			

			if (forward != null) {
				if (forward.isRedirect()) {
					response.sendRedirect(forward.getPath());
				} else {
					RequestDispatcher dispatcher = request
							.getRequestDispatcher(forward.getPath());
					dispatcher.forward(request, response);
				}
			}
			
	}
	
	

}
